package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.BookWithUserService;
import com.example.demo.commom.Result;
import com.example.demo.entity.BookWithUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 借阅状态
 */
@RestController
@RequestMapping("/api/bookwithuser")
@Slf4j
public class BookWithUserController {

    @Resource
    BookWithUserService bookWithUserService;

    //
//    @PostMapping
//    public Result<?> save(@RequestBody Book Book){
//        BookMapper.insert(Book);
//        return Result.success();
//    }

//    //    批量删除
//    @PostMapping("/deleteBatch")
//    public  Result<?> deleteBatch(@RequestBody List<Integer> ids){
//        BookMapper.deleteBatchIds(ids);
//        return Result.success();
//    }
//    @PutMapping
//    public  Result<?> update(@RequestBody Book Book){
//        BookMapper.updateById(Book);
//        return Result.success();
//    }
//    @DeleteMapping("/{id}")
//    public Result<?> delete(@PathVariable Long id){
//        BookMapper.deleteById(id);
//        return Result.success();
//    }

    /**
     * 将图书添加到借阅中
     *
     * @param BookWithUser
     * @return
     */
    @PostMapping("/insertNew")
    public Result<?> insertNew(@RequestBody BookWithUser BookWithUser) {
        log.info("将图书添加到借阅中：{}", BookWithUser);
        bookWithUserService.insert(BookWithUser);
        return Result.success();
    }

    /**
     * 续借书籍
     *
     * @param BookWithUser
     * @return
     */
    @PostMapping
    public Result<?> update(@RequestBody BookWithUser BookWithUser) {
        log.info("续借书籍：{}", BookWithUser);
        bookWithUserService.update(BookWithUser);
        return Result.success();
    }

    /**
     * 删除这条借阅状态数据
     *
     * @param BookWithUser
     * @return
     */
    @PostMapping("/deleteRecord")
    public Result<?> deleteRecord(@RequestBody BookWithUser BookWithUser) {
        log.info("删除一条借阅状态记录：{}", BookWithUser);
        bookWithUserService.deleteRecord(BookWithUser);
        return Result.success();
    }

    /**
     * 批量删除借阅状态数据
     *
     * @param BookWithUsers
     * @return
     */
    @PostMapping("/deleteRecords")
    public Result<?> deleteRecords(@RequestBody List<BookWithUser> BookWithUsers) {
        log.info("批量删除借阅状态记录：{}", BookWithUsers);
        bookWithUserService.deleteRecords(BookWithUsers);
        return Result.success();
    }

    /**
     * 分页查询借阅记录
     *
     * @param pageNum
     * @param pageSize
     * @param search1
     * @param search2
     * @param search3
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        log.info("分页查询借阅状态数据，页码：{},总页数：{},图书编号：{},书名：{},借阅者：{}", pageNum, pageSize, search1, search2, search3);
        Page<BookWithUser> BookPage = bookWithUserService.selectPage(pageNum, pageSize, search1, search2, search3);
        return Result.success(BookPage);
    }
}
